<?php
/**
 * Action object to be used with the nuc_Controller classes.
 * 
 * @package		Nuclide-Mvc
 * @author		Mats Lundberg
 * @version		$Id$
 */

nuc_Loader::loadClass('mvc.nuc_Filter_Input');

/**
 * Action object to be used with the nuc_Controller classes.
 * 
 * @package		Nuclide-Mvc
 */
abstract class nuc_Controller_Action_Object {
	/**
	 * This is a compatibility solution for old versions that
	 * support global view variables.
	 * 
	 * @staticvar bool
	 */
	protected static $_globalVars = true;	
	/**
	 * Arguments
	 * 
	 * @var array
	 */
	protected $_arguments;
	/**
	 * Controller action
	 * 
	 * @var string
	 */	
	private   $_controllerAction;
	/**
	 * Is the execution cancelled?
	 * 
	 * @var bool
	 */	
	private	  $_cancelled		= false;
	/**
	 * Input filter defintions
	 * 
	 * @var array
	 */		
	protected $inputFilter		= array();
	/**
	 * Input validator defintions
	 * 
	 * @var array
	 */		
	protected $inputValidator	= array();
	/**
	 * Input method
	 * 
	 * @var string
	 */		
	protected $inputType		= 'GET';
	/**
	 * Input results
	 * 
	 * @var nuc_Filter_Input
	 */
	protected $input;
	/**
	 * Action object view
	 * 
	 * @var nuc_View
	 */
	protected $view;
	
	/**
	 * Globally allows or disallows global view variables. This is mostly a compatibility solution. 
	 * 
	 * @param	bool	Allow or disallow
	 * @static
	 */
	static public function setGlobalVars( $status ) {
		self::$_globalVars = $status;
	}	
	
	/**
	 * Public constructor
	 * 
	 * @param	array					Arguments
	 * @param 	nuc_Controller_Action	Parent action
	 */
	public function __construct( array $args, &$parent ) {
		// Set controller details
		$this->_arguments			= $args;
		$this->_controllerAction	= &$parent;
		$this->view 				= $parent->view;
		
		if( self::$_globalVars == false ) {
			$this->view->clearVars();
		}
		
		// Execute action initializer
		$this->__init();
		
		// Set input filter
		$this->input = new nuc_Filter_Input(
			$this->inputFilter, 
			$this->inputValidator, 
			$this->inputType != 'GET' ? $_POST : $_GET 
		);
		
		// Set default escape filter
		Zend_Loader::loadClass('Zend_Filter_HtmlEntities');
		$this->input->setDefaultEscapeFilter( new Zend_Filter_HtmlEntities( ENT_COMPAT, 'utf-8' ) );
	}
		
	/**
	 * Default init handler
	 */
	public function __init() {}
	/**
	 * Default finish handler
	 */
	public function __finish() {}
	
	/**
	 * Cancel the action execution. Can be used e.g. to automatize caching 
	 */
	protected function _cancel() {
		$this->_cancelled = true;
	}
	
	/**
	 * Tells if the action execution has been cancelled
	 */
	public function isCancelled() {
		return $this->_cancelled;
	}
	
	/**
	 * Bubble method call to Controller_Action
	 *
	 * @param	string	Method name
	 * @param	array	Method arguments
	 * @return	mixed
	 */
	public function __call( $name, $args ) {
		if( method_exists( $this->_controllerAction, $name ) ) {
			return call_user_func_array( array( &$this->_controllerAction, $name ), $args );
		} else { 
			throw new Zend_Controller_Action_Exception('Method does not exist in Action Object.');
		}
	}
	
	/**
	 * Returns the desired controller action helper
	 * 
	 * @param	string		Helper name
	 * @return	Controller action helper
	 */
	public function getHelper( $helper ) {
		return $this->_controllerAction->getHelper( $helper );
	}
	
	/**
	 * Object setter
	 * 
	 * @param	string		Variable name
	 * @param	mixed		Variable value
	 */	
	public function __set( $name, $value ) {
		$this->_arguments[ $name ] = $value;
	}
	
	/**
	 * Object getter
	 * 
	 * @param	string		Variable name
	 * @return	Variable value
	 */	
	public function __get( $name ) {
		return $this->_arguments[ $name ];
	}
	
	/**
	 * Required execution method for extending classes
	 * 
	 * @abstract
	 */
	abstract function execute();
}
?>